/**
 * 基于dropzone的文件上传插件
 * @param $
 */(function($) {
	 Dropzone.autoDiscover = false;
	$.fn.ash_dz = function(options) {
        var opts = $.extend({}, $.fn.ash_dz.defaults, options);
        return this.each(function() {
            var _$this = $(this),_$newE;
            //_$this.attr("type","hidden");
            _$newE =$("<div class='dropzone' id='dropzone"+_$this.attr("id")+"'></div>");
            _$showDIV =$("<div class='imgshow' id='imgshow"+_$this.attr("id")+"'+><div class='col-lg-12 showArea'></div><div class='col-lg-12 showtools'><span style='color:#c00;'>点击图片查看大图</span><a class='btn btn-info togglesBtn' ><i class='fa fa-edit'></i>&nbsp;修 改</a></div></div> ")
            _$this.after(_$showDIV).after(_$newE);
            if(_$this.val()==null||_$this.val()=="")
            {
            	_$showDIV.hide();
            }
            else
            {
            	_$newE.hide();
            }
            _$showDIV.find("a.togglesBtn").on("click",function(){
            	_$this.val("");
            	_$showDIV.hide();
            	_$newE.show();
            });
            _$showDIV.on("click",function(){
            	 layer.photos({
                     photos: _$showDIV
                 });
            });
    		var myDropzone = new Dropzone("#dropzone"+_$this.attr("id"), { 
    			url:opts.url,
    			maxFilesize: opts.maxFilesize, 
    			maxFiles:opts.maxFiles,
    			addRemoveLinks:opts.addRemoveLinks,     
    	    	acceptedFiles:opts.acceptedFiles,
    	    	dictDefaultMessage: "点击或拖拽文件到这里上传",
      		  	dictFallbackMessage: "您的浏览器不支持此上传插件，请使用Chrome 7+、Firefox 4+、IE 10+、Opera 12+ (Version 12 for MacOS is disabled because their API is buggy)、Safari 6+浏览器。",
      		  	dictFallbackText: "请使用默认表单上传.",
      		  	dictFileTooBig: "文件大小({{filesize}}MiB)超出最大限制 {{maxFilesize}}MiB。",
      		  	dictInvalidFileType: "抱歉，您不能上传此类文件",
      		  	dictResponseError: "服务器发送 {{statusCode}}错误.",
      		  	dictCancelUpload: "取消上传",
      		  	dictCancelUploadConfirmation: "确定你要取消本次上传么?",
      		  	dictRemoveFile: "删除",
      		  	dictRemoveFileConfirmation: "确认要删除本文件么？",
      		  	dictMaxFilesExceeded: "您上传的文件已经达到设定上限，不能继续上传了，抱歉。",
    		});
    		myDropzone.on("error",function(file,message){
    			layer.msg("上传发生错误："+message);
    		});
    		myDropzone.on("success",function(file, responseText){
    			file.url=responseText.url;
    			if(_$this.val()==null||_$this.val()=="")
    			{
    				_$this.val(responseText.url);
    			}
    			else
    			{
    				_$this.val(_$this.val()+","+responseText.url);
    			}
    			
    		});
    		myDropzone.on("removedfile", function(file) {
    			if(file.url)
				  {
					  var imgurl =_$this.val();
					  _$this.val(imgurl.replace(file.url+",",""));
				  }
    		});
    		myDropzone.on("addedfile", function(file) {
    			  if(myDropzone.files.length>opts.maxfiles)
    			  {
    				  layer.msg("上传文件数目达到最高限制！不能再上传图片");
    				  myDropzone.removeFile(file);
    				  if(file.url)
    				  {
    					  var imgurl =_$this.val();
    					  _$this.val(imgurl.replace(file.url+",",""));
    				  }
    			  }	
    			  if(opts.deleteAble)
    			  {
    				  file.previewElement.addEventListener("click", function() {
    					  myDropzone.removeFile(file);
        				  if(file.url)
        				  {
        					  var imgurl =_$this.val();
        					  _$this.val(imgurl.replace(file.url+",",""));
        				  }
    	    		  }); 
    			  }
    		});
        });
    };
    $.fn.ash_dz.defaults ={
    	maxfiles:16, //最大上传数目
    	maxFilesize:2, //mb 上传文件最大限制
    	url:_ctx+"/upload/dropzone", //上传地址
    	deleteAble:true,           //是否点击删除
    	addRemoveLinks:true,       //是否显示删除链接
    	acceptedFiles:"image/*"      //文件类型限制  Eg.: image/*,application/pdf,.psd.
    };
})(jQuery);